<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的{{ titleBar }}</block>
		</cu-custom>
		<view
			class="flex text-xl padding justify-between align-center"
			:class="'bg-' + theme.backgroundColor">
			<view class="text-df">
				<view
					>当前拥有<br /><text class="text-bold points">{{ userInfo[infoType] }}</text></view
				>
				<!-- <navigator class="cu-btn sm  text-sm margin-top-sm round line-white" url="/pages/signrecord/signrecord-info/index" hover-class="none">签到领积分</navigator> -->
			</view>
			<image
				class="points-image"
				src="https://joolun-plus-1313274050.cos.ap-nanjing.myqcloud.com/joolun-plus-uniapp/points-bg.png">
			</image>
		</view>
		<view class="cu-list menu bg-white points-list" v-if="infoType == 'pointsCurrent'">
			<view class="margin-top-sm text-center">
				<text>用户{{ titleBar }}明细</text>
			</view>
			<view class="cu-item" v-for="(item, index) in pointsRecord" :key="index">
				<view class="content padding-tb-sm">
					<view class="text-sm padding-right-sm">{{ item.description }}</view>
					<view class="text-gray text-sm" v-if="item.createTime">{{ item.createTime }}</view>
				</view>
				<view class="action">
					<text class="text-bold text-green margin-right-xs" v-if="item.amount > 0"
						>+{{ item.amount }}</text
					>
					<text class="text-bold text-red margin-right-xs" v-if="item.amount < 0">{{
						item.amount
					}}</text>
				</view>
			</view>
		</view>
		<view class="cu-list menu bg-white points-list" v-if="infoType == 'couponNum'">
			<view class="margin-top-sm text-center">
				<text>用户{{ titleBar }}明细</text>
			</view>
			<view class="cu-item" v-for="(item, index) in pointsRecord" :key="index">
				<view class="content padding-tb-sm">
					<view class="text-sm padding-right-sm">{{ item.couponName }}</view>
					<view class="text-gray text-sm">{{ item.createTime }}</view>
				</view>
				<view class="action">
					{{ item.checkFlag == "1" ? "已核销" : "未核销" }}
				</view>
			</view>
		</view>
		<view class="cu-list menu bg-white points-list" v-if="infoType == 'redPrice'">
			<view class="margin-top-sm text-center">
				<text>用户{{ titleBar }}明细</text>
			</view>
			<view class="cu-item" v-for="(item, index) in pointsRecord" :key="index">
				<view class="content padding-tb-sm">
					<view class="text-sm padding-right-sm">{{ item.wishing }}</view>
					<view class="text-gray text-sm">{{ item.createTime }}</view>
				</view>
				<view class="text-red text-price" v-if="infoType == 'redPrice'">
					{{ item.totalAmount || 0 }}
				</view>
				<view class="action" v-if="infoType != 'redPrice'">
					{{ item.transferAmount }}
				</view>
			</view>
		</view>
		<view class="cu-list menu bg-white points-list" v-if="infoType == 'prizeNum'">
			<view class="margin-top-sm text-center">
				<text>用户{{ titleBar }}明细</text>
			</view>
			<view class="cu-item" v-for="(item, index) in pointsRecord" :key="index">
				<view class="content padding-tb-sm">
					<view class="text-sm padding-right-sm"
						>在{{ item.liveName }} 获得 {{ item.lotteryName }}</view
					>
					<view class="text-gray text-sm">{{ item.createTime }}</view>
				</view>
				<!-- <view>{{ item.lotteryMsg }}</view> -->
				<view class="action">
					{{ item.checkFlag == "1" ? "已核销" : "未核销" }}
				</view>
			</view>
		</view>
		<view :class="'cu-load bg-gray ' + (loadmore ? 'loading' : 'over')"></view>
	</view>
</template>

<script>
const util = require("utils/util.js");
const app = getApp();
import api from "utils/api";

export default {
	data() {
		return {
			CustomBar: this.CustomBar,
			theme: app.globalData.theme, //全局颜色变量
			page: {
				current: 1,
				size: 15,
				//升序字段
			},
			parameter: {},
			loadmore: true,
			pointsRecord: [],
			userInfo: {
				pointsCurrent: "",
			},
			infoType: "",
			titleBar: "",
			// apiLiist: [
			// 	{
			// 		couponNum: "liveusersetreward",
			// 		redPrice: "livecouponinfo",
			// 		pointsCurrent: "pointsRecordPage",
			// 		prizeNum: "livelotteryset",
			// 	},
			// ],
			// type 1 积分 2 红包 3 优惠券 4 奖品
		};
	},

	components: {},
	computed: {},
	props: {},

	onLoad(options) {
		// 保存别人分享来的 userCode
		this.infoType = options.type;
		this.titleBar = this.typeDictionary(options.type);
		// util.saveSharerUserCode(options);
		app.initPage().then((res) => {
			this.userInfoGet();
			this.refresh();
		});
	},

	onShow(options) {},

	onReachBottom() {
		if (this.loadmore) {
			this.page.current = this.page.current + 1;
			// this.pointsRecordPage();
			this.init();
		}
	},

	methods: {
		typeDictionary(type) {
			let obj = {
				pointsCurrent: "积分",
				redPrice: "红包",
				couponNum: "优惠券",
				prizeNum: "奖品",
			};
			return obj[type];
		},
		//刷新
		refresh() {
			this.page.current = 1;
			this.pointsRecord = [];
			this.init();
		},
		// 初始化
		init() {
			this.loadmore = true;
			if (this.infoType == "pointsCurrent") {
				this.pointsRecordPage();
			}
			if (this.infoType == "redPrice") {
				this.liverewardrecord();
			}
			if (this.infoType == "couponNum") {
				this.livecouponrecord();
			}
			if (this.infoType == "prizeNum") {
				this.livelotteryjoinrecord();
			}
		},
		// pointsRecordPage() {
		// 	api
		// 		.liverewardrecord(
		// 			Object.assign({ rewardType: "1" }, this.page, util.filterForm(this.parameter)),
		// 		)
		// 		.then((res) => {
		// 			let pointsRecord = res.data.records;
		// 			this.pointsRecord = [...this.pointsRecord, ...pointsRecord];
		// 			if (pointsRecord.length < this.page.size) {
		// 				this.loadmore = false;
		// 			}
		// 		});
		// },
		liverewardrecord() {
			api
				.liverewardrecord(
					Object.assign(
						{
							rewardType: "2",
						},
						this.page,
						util.filterForm(this.parameter),
					),
				)
				.then((res) => {
					let pointsRecord = res.data.records;
					this.pointsRecord = [...this.pointsRecord, ...pointsRecord];
					if (pointsRecord.length < this.page.size) {
						this.loadmore = false;
					}
				});
		},
		livecouponrecord() {
			api
				.livecouponrecord(
					Object.assign(
						{
							rewardType: "3",
						},
						this.page,
						util.filterForm(this.parameter),
					),
				)
				.then((res) => {
					let pointsRecord = res.data.records;
					this.pointsRecord = [...this.pointsRecord, ...pointsRecord];
					if (pointsRecord.length < this.page.size) {
						this.loadmore = false;
					}
				});
		},
		livelotteryjoinrecord() {
			api
				.livelotteryjoinrecord(
					Object.assign(
						{
							rewardType: "4",
						},
						this.page,
						util.filterForm(this.parameter),
					),
				)
				.then((res) => {
					let pointsRecord = res.data.records;
					this.pointsRecord = [...this.pointsRecord, ...pointsRecord];
					if (pointsRecord.length < this.page.size) {
						this.loadmore = false;
					}
				});
		},
		//获取商城用户信息
		userInfoGet() {
			api.userInfoGet().then((res) => {
				this.userInfo = res.data;
			});
		},

		pointsRecordPage() {
			api
				.pointsRecordPage(
					Object.assign({ rewardType: "1" }, this.page, util.filterForm(this.parameter)),
				)
				.then((res) => {
					let pointsRecord = res.data.records;
					this.pointsRecord = [...this.pointsRecord, ...pointsRecord];
					if (pointsRecord.length < this.page.size) {
						this.loadmore = false;
					}
				});
		},
	},
};
</script>

<style>
.points {
	font-size: 80rpx;
	background: linear-gradient(to bottom, #fff, #f19972);
	-webkit-background-clip: text;
	background-clip: text;
	color: transparent;
}

.points-image {
	width: 372rpx;
	height: 300rpx;
}

.points-list {
	border-radius: 40rpx;
	margin-top: -40rpx;
}
</style>
